import Vue from 'vue'
import Vuex from 'vuex'
import App from './App.vue'
Vue.use(Vuex)



//加载router
import VueRouter from 'vue-router'
Vue.use(VueRouter)

//导入mint-ui
import MintUI from 'mint-ui'//导入
import 'mint-ui/lib/style.css'
// import 'mint-ui/lib/index.js'
Vue.use(MintUI)
//按需求导入
// import {Header,Swipe,SwipeIem,button} from 'mint-ui'
// Vue.component(Header.name, Header)
//安装vue-preiew
import VuePreview from 'vue-preview'
 Vue.use(VuePreview)

//使用iconfont矢量图标库
import './lib/icons/iconfont.css'
import './lib/icons/iconfont.js'

//导入mui不支持npm安装
import './lib/mui/css/mui.min.css'
import './lib/mui/css/icons-extra.css'

//导入vue-resource
import VueResource from 'vue-resource'
Vue.use(VueResource)

//设置请求的跟组件
Vue.http.options.root='https://www.apiopen.top';
//全局设置post时候表单数据格式  application/x-www-form-urlencoded
Vue.http.options.emlulateJSON=true;

//导入moment
import moment from 'moment'
//定义全局过滤器
Vue.filter('dataFormt',function(dataStr,pattern="YYYY-MM-DD HH:mm:ss"){
  return moment(dataStr).format(pattern)
})

Vue.config.productionTip = false

//导入router.js
import router from './router.js'

//每次刚进去网站调用main.js
var car=JSON.parse(localStorage.getItem('car')||'[]')
//vuex
const store = new Vuex.Store({
  state: {
    //如果在组件中想要访问stroe中的数据
    //$store.state.count
    count: 0,
    car:car
  },
  mutations: {
    increment (state) {
      state.count++
    },
    minus(state){
      state.count--
    },
    addtocar(state,goodsinfo){
      // var flag=flase

      // state.car.some(item=>{
      //   if(item.id==goodsinfo.id){
      //     item.count+=parseInt(goodsinfo.count)
      //     flag=true
      //     return true
      //   }
      // })
      // if(!flag){
        state.car.push(goodsinfo)


        localStorage.setItem('car',JSON.stringify(state.car))
    }

  },
  getters:{
    // optCount:function(state){
    //   return '当前的数值为:'+state.count
    // }
    getallcount(state){
      var c=0;
      state.car.forEach(item=>{
        c+=item.counts
      })
      return c
    },
    // getgoodsselected(state){
    //   var o={}
    //   state.car.forEach(item=>{
    //     o[item.id]=item.serlected
    //   })
    //   return o;
    // }
  }
})

new Vue({
  render: h => h(App),
  router, //挂载gogogo
  store//挂载vuex
}).$mount('#app')
